//
//  VSFindGiftPushCollectionViewController.m
//  UI项目2.0
//
//  Created by dlios on 15/7/17.
//  Copyright (c) 2015年 星际科技. All rights reserved.
//

#import "VSFindGiftPushCollectionViewController.h"
#import "VSFrameGrand.h"
#import "VSFindGiftPushCollectionViewCell.h"
#import "VSFindOtherPushViewController.h"

@interface VSFindGiftPushCollectionViewController () <UICollectionViewDataSource, UICollectionViewDelegate,VSFindGiftPushCollectionViewCellDelegate>
@property (nonatomic, assign) CGFloat LayoutWidth;
@property (nonatomic, assign) CGFloat LayoutHeight;
@property (nonatomic, assign) CGFloat CellLabelHeight;
@property (nonatomic, assign) CGFloat CellPhotoHeight;


@end

@implementation VSFindGiftPushCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor lightGrayColor];
    self.navigationItem.title = @"详情";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-zuofanhuijiantou.png"] style:UIBarButtonItemStylePlain target:self action:@selector(GiftbackAction)];
    [self handleData];
    self.parentViewController.tabBarController.tabBar.hidden = YES;
}

- (void)GiftbackAction
{
    [self.navigationController popViewControllerAnimated:YES];
    self.parentViewController.tabBarController.tabBar.hidden = NO;
}

- (void)createView
{
    UICollectionViewFlowLayout *Layout = [[[UICollectionViewFlowLayout alloc]init]autorelease];
    Layout.sectionInset = UIEdgeInsetsMake(5, 2, 2, 2);
    
    Layout.itemSize = CGSizeMake(self.LayoutWidth, self.LayoutHeight);
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:Layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    [collectionView registerClass:[VSFindGiftPushCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    
    UIImageView *backGroundImage = [[UIImageView alloc]initWithFrame:self.view.bounds];
    backGroundImage.image = [UIImage imageNamed:@"background003.png"];
    collectionView.backgroundView = backGroundImage;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    VSFindGiftPushCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.PassHeight = self.CellLabelHeight;
    cell.photoHeight = self.CellPhotoHeight;
    cell.model = self.giftModel;
    cell.delegate = self;
    
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

-(void)didSelectButton:(VSFindGiftModel *)PassModel{

    VSFindOtherPushViewController *new = [[[VSFindOtherPushViewController alloc]init] autorelease];
    new.FromGiftToOtherModel = PassModel;
    [self presentViewController:new animated:YES completion:^{
        
    }];
    
}


- (void)handleData
{
    CGFloat buttonHeight = 0;
    if ([self.giftModel.source_link containsString:@"taobao"] || [self.giftModel.source_link containsString:@"weibo"]) {
        buttonHeight = 45 + 14;
    }
    CGFloat WantWIDTH = SCREEN_WIDTH - 30;
    CGFloat LabelHeight = [[self class] heightWithText:self.giftModel.msg];
    self.LayoutWidth = WantWIDTH;
    self.LayoutHeight = WantWIDTH / [[self.giftModel.photo objectForKey:@"width"] floatValue] * [[self.giftModel.photo objectForKey:@"height"] floatValue] + LabelHeight + 26 + buttonHeight;
    self.CellLabelHeight = LabelHeight + 20;
    self.CellPhotoHeight = WantWIDTH / [[self.giftModel.photo objectForKey:@"width"] floatValue] * [[self.giftModel.photo objectForKey:@"height"] floatValue];
    [self createView];
    
}

+ (CGFloat)heightWithText:(NSString *)text
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:18]};
    CGSize size = CGSizeMake((SCREEN_WIDTH - 30) , 2000);
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
